package us.icitap.cm.ui.policecase;

import java.util.List;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.business.entities.Evidence;
import us.icitap.cm.business.entities.Evidence.EvidenceTypeEnum;
import us.icitap.cm.business.entities.Weapon;
import us.icitap.cm.listeners.AppEntityListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustTextArea;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.AppEntityLayout;
import us.icitap.cm.utils.ProjectLists;
import us.icitap.utils.Result;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

public class WeaponDataLayout extends AppEntityLayout{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4819326275585074176L;

	
	private Weapon weapon;
	private final CustBeanFieldGroup<Weapon> binder = new CustBeanFieldGroup<Weapon>(Weapon.class);
	protected AppEntityListener listener;
	final CustComboBox weaponClassficationSelect = new CustComboBox("klasifikimi i armes");
	final CustComboBox weaponTypeSelect = new CustComboBox("Tipi i armes");
	final CustComboBox weaponModelSelect = new CustComboBox("Modeli i armes");
	final CustTextArea notesTextArea = new CustTextArea("Detaje te armes");
	
	private final CheckBox evidenceCheckBox = new CheckBox("Eshte prove materiale");
	
	final CustomFormLayout cfl = new CustomFormLayout("450px", "Arme te perfshira ne vendngjarje", "Reset", "Ruaj");
	
	boolean editableVisible = false;
	boolean readOnlyVisible = false;

	private final EvidenceEmbeddableDataLayout evidenceEmbDataLayout = new EvidenceEmbeddableDataLayout();
	
	final TabSheet tabSheet = new TabSheet();
	
	public WeaponDataLayout() {
		this.setMargin(true);
		
		FormLayout fl = new FormLayout();
		fl.addStyleName("cmLayout");
		fl.setMargin(true);
		fl.addComponent(this.weaponClassficationSelect);
		fl.addComponent(this.weaponTypeSelect);
		fl.addComponent(this.weaponModelSelect);
		fl.addComponent(this.notesTextArea);
		
		fl.addComponent(this.evidenceCheckBox);
		
		notesTextArea.setWidth("20em");
		
		tabSheet.addTab(fl, "Te dhenat");
		

		cfl.addComponent(tabSheet);
		cfl.addFooter();
		weaponClassficationSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.WeaponClassification, false)));
		
		
		weaponClassficationSelect.setImmediate(true);
		weaponClassficationSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				weaponTypeSelect.removeAllItems();
				if(weaponClassficationSelect.getValue() != null){
					AppList parent = (AppList) weaponClassficationSelect.getValue();
					Result<List<AppList>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getAppListChildren(parent);
					if(result.isSucceeded() && result.getObject() != null && !result.getObject().isEmpty()){
						weaponTypeSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, result.getObject()));
						
					}
				}
				
			}
		});
		
		
		weaponTypeSelect.setImmediate(true);
		weaponTypeSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				weaponModelSelect.removeAllItems();
				if(weaponTypeSelect.getValue() != null){
					AppList parent = (AppList) weaponTypeSelect.getValue();
					Result<List<AppList>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getAppListChildren(parent);
					if(result.isSucceeded() && result.getObject() != null && !result.getObject().isEmpty()){
						weaponModelSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, result.getObject()));
						
					}
				}
				
			}
		});
		
		
		
		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				try {
					binder.commit();
					if(WeaponDataLayout.this.listener != null){
						if(oldAppEntity == null){
							// Eshte entitet i ri
							if((evidenceCheckBox.getValue().booleanValue())){
								weapon.getAppEntity().setEvidence(evidenceEmbDataLayout.getEvidence());	
							}
							WeaponDataLayout.this.listener.addAppEntityToReport(weapon.getAppEntity());
						}
						else{
							//Eshte entitet ekzistues
							weapon.getAppEntity().setEvidence(evidenceEmbDataLayout.getEvidence());
							if(!oldAppEntity.getWeapon().myEquals(weapon)){
								//Entiteti ka ndryshuar");
								if(weapon.getAppEntity().getEvidence() != null && 
										weapon.getAppEntity().getEvidence().myEquals(oldAppEntity.getEvidence())){
									//Nuk ka ndryshuar prova");
									weapon.getAppEntity().setEvidence(oldAppEntity.getEvidence());
								}
								weapon.getAppEntity().setRecordUser(CaseManagementUI.getUser().getUsername());
								WeaponDataLayout.this.listener.updateAppEntityIntoReport(oldAppEntity, weapon.getAppEntity());
							}
							else
							{
								//Entiteti nuk k ndryshuar, as prova
//								oldAppEntity.replaceEntityReportAssociationList(weapon.getAppEntity().getReportAssociationList());
								WeaponDataLayout.this.listener.refresh(oldAppEntity);
							}
							addAppEntity();
						}
							
						
						
					}
				} catch (CommitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		binder.bind(this.weaponClassficationSelect, "classification");
		binder.bind(this.weaponTypeSelect, "weaponType");
		binder.bind(this.weaponModelSelect, "weaponModel");
		binder.bind(this.notesTextArea, "notes");
		
		evidenceCheckBox.setImmediate(true);
		evidenceCheckBox.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				if(evidenceCheckBox.getValue().booleanValue()){
					// Entiti shoqerohet me prove
					tabSheet.addTab(evidenceEmbDataLayout, "Prove materiale");
					
					if(oldAppEntity != null && oldAppEntity.getEvidence() != null){
						// Entiteti ekzistues ka prove ekzistuese te lidhur me te
						weapon.getAppEntity().setEvidence(new Evidence(oldAppEntity.getEvidence()));
						evidenceEmbDataLayout.loadEvidence(weapon.getAppEntity().getEvidence(),true);
					}
					else{
						// Entiteti ekzistues nuk ka prove ekzistues te lidhur me te
						// Krijohet prove e re 
						evidenceEmbDataLayout.createEvidence(EvidenceTypeEnum.MATERIAL);
					}
				}
				else{
					// Entiteti nuk ka prove ekzistuese te lidhur me te
					weapon.getAppEntity().setEvidence(null);
					tabSheet.removeComponent(evidenceEmbDataLayout);
				}	
			}
		});
		
	}

	public void showEditableForm(){
		if(editableVisible) return ;
		
		this.removeAllComponents();
		this.addComponent(cfl);
		
		editableVisible = true;
		readOnlyVisible = false;
	}
	

	public void showReadOnlyForm(){
		if(readOnlyVisible) return;
		this.removeAllComponents();
		VerticalLayout vl = ViewCaseLayout.showWeapon(weapon);
		vl.setCaption(EntityTypeEnum.DOCUMENT.getCaption());
		this.addComponent(vl);
		editableVisible = false;
		readOnlyVisible = true;
	}

	@Override
	public void addAppEntity() {
		this.tabSheet.setSelectedTab(0);
		AppEntity appEntity = new AppEntity(CaseManagementUI.getUser().getUsername(), EntityTypeEnum.WEAPON);
		weapon = appEntity.getWeapon();
		binder.setItemDataSource(weapon);
		this.showEditableForm();

	}
	
	@Override
	public void addAppEntityListener(AppEntityListener listener) {
		this.listener = listener;
		
	}


	private AppEntity oldAppEntity;
	
	
	@Override
	public void loadAppEntity(AppEntity appEntity, boolean editable) {

		oldAppEntity = appEntity;
		
		AppEntity newAppEntity = new AppEntity(appEntity);
		
		weapon = newAppEntity.getWeapon();
		binder.setItemDataSource(weapon);

		if(newAppEntity.getEvidence()!= null){
			evidenceCheckBox.setValue(new Boolean(true));
			evidenceEmbDataLayout.loadEvidence(newAppEntity.getEvidence(), true);
			tabSheet.addTab(evidenceEmbDataLayout, "Prove materiale");
		}
		else{
			evidenceCheckBox.setValue(new Boolean(false));
		}
		
		if(editable) 
			this.showEditableForm();
		
		else
			this.showReadOnlyForm();
	}

}
